# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
from scrapy.pipelines.images import ImagesPipeline
from scrapy import Request
from scrapy.exceptions import DropItem
import os


class MayaPipeline(object):
    def process_item(self, item, spider):
        return item


class ImagePipeline(ImagesPipeline):
    def get_media_requests(self, item, info):
        for url in item['img_urls']:
            yield Request(url=url, meta={'item': item})

    def file_path(self, request, response=None, info=None):
        dir_path = request.meta['item']['img_title']
        file_name = request.url.split('/')[-1]
        file_path = os.path.join(dir_path, file_name)
        return file_path

    def item_completed(self, results, item, info):
        image_path = [x['path'] for ok, x in results if ok]
        if not image_path:
            raise DropItem('Image Downloaded Failed')
        return item
